<template>
  <div class="login-page">
    <div class="login-container">
      <el-card class="box-card">
        <div class="text-center">
          <h3>管理员登录</h3>
        </div>
        <el-form :model="form" :rules="rules" ref="loginForm" label-position="top">
          <el-form-item label="账号" prop="username">
            <el-input v-model="form.username" placeholder="请输入账号" @keypress="onlyNumberInput"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入密码" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="form.remember">记住密码</el-checkbox>
          </el-form-item>
          <el-form-item>
            <div class="button-group">
              <el-button type="success" @click="onSubmit">登录</el-button>
              <el-button type="primary" @click="toMemberLogin">转到会员登录</el-button>
            </div>
          </el-form-item>
        </el-form>
        <div v-if="msg" class="error-msg">{{ msg }}</div>
      </el-card>
    </div>
  </div>
</template>


<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();

const form = ref({
  username: '',
  password: '',
  remember: false,
});

const rules = {
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
};

const loginForm = ref(null);
const msg = ref('');

const onSubmit = () => {
  loginForm.value.validate((valid) => {
    if (valid) {
      axios.post('/server/Admin/AdminLogin', {
        username: form.value.username,
        password: form.value.password,
      })
      .then(response => {
        const data = response.data;
        if (data.code === 200) {
          const token = response.data.data.token
          localStorage.setItem('token',token)
          ElMessage.success('登录成功');
          router.push('/AdminMain/AdminHome');
        } else {
          msg.value = data.message || '登录失败';
        }
      })
      .catch(() => {
        msg.value = '登录请求失败';
      });
    }
  });
};


const toMemberLogin = () => {
  router.push('/memberLogin'); // Vue Router跳转
};

const onlyNumberInput = (e) => {
  if (!(e.keyCode >= 48 && e.keyCode <= 57)) {
    e.preventDefault();
  }
};
</script>

<style scoped>
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden; /* 禁止滚动条 */
}

.login-page {
  width: 100vw;
  height: 100vh;
  background: url('/img/background.jpeg') no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-container {
  width: 400px;
}

.box-card {
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

.button-group {
  display: flex;
  justify-content: space-between;
}

.error-msg {
  color: red;
  text-align: center;
  margin-top: 20px;
}

</style>
